package com.zahb.qadx.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.zahb.qadx.buriedpoint.BuriedPoint;
import com.zahb.qadx.buriedpoint.TheBasicParametersBean;

import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created on 2018/12/03 17:48.
 *
 * @author zhoudawei
 */
public abstract class BaseFragmentExt extends Fragment {

    protected Bundle mArguments;
    protected View mRootView;
    protected Unbinder mUnbinder;
    private CompositeDisposable mCompositeDisposable;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        TheBasicParametersBean.Properties  properties   =new TheBasicParametersBean.Properties();

        Gson mGson=new Gson();
        String json= mGson.toJson(BuriedPoint.buriedPoint(getActivity(),"", getClass().getName(),"当前页面Fragment"));
        Logger.d(json);
        super.onCreate(savedInstanceState);
//        Logger.addLogAdapter(new AndroidLogAdapter() {
//            @Override public boolean isLoggable(int priority, String tag) {
//                return true; //开启
//            }
//        });
        mArguments = getArguments();
        if (mArguments == null)
            mArguments = new Bundle();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null)
            mRootView = inflater.inflate(getLayout(), container, false);
        initViews(mRootView);
        return mRootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRootView = view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        hideProgressDialog();
        hideBindToast();
        mRootView = null;
        if (mUnbinder != null)
            mUnbinder.unbind();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        clearDisposable();
    }

    @LayoutRes
    abstract protected int getLayout();

    abstract protected void initViews(View rootView);

    /**
     * 添加订阅
     */
    protected void addDisposable(Disposable disposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(disposable);
    }

    /**
     * 取消所有订阅
     */
    private void clearDisposable() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear();
        }
    }

    public void showProgressDialog(@Nullable String text) {
        if (getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).showProgressDialog(text);
        }
    }

    public void hideProgressDialog() {
        if (getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).hideProgressDialog();
        }
    }

    public void showBindToast(@StringRes int stringId) {
        showBindToast(getString(stringId));
    }

    public void showBindToast(@Nullable String text) {
        if (getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).showBindToast(text);
        }
    }

    private void hideBindToast() {
        if (getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).hideBindToast();
        }
    }
}
